package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.CommunityDao;
import cn.hs.ph.entity.Community;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CommunityDaoImpl implements CommunityDao {
    @Override
    public int insert(Community community) {
        String sql = "insert into community values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object [] obj = new Object[]{
                community.getCommunityName(),
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                new Date(),
                community.getCommunityShopCount(),
                community.getCommunityAreaCode(),
                community.getCommunityCoordinates(),
                community.getCommunityStatus(),
                community.getCommunityUserCount(),
                community.getDistance()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int delete(int communityId) {
        String sql = "delete from community where communityId=?";
        Object obj[]={communityId};
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int update(Community community) {
        String sql = "update community set communityAddress=?,communityAreaCode=?,communityCity=?,communityCoordinates=?,communityDistrict=?,communityId=?,communityLatitude=?,communityLongitude=?,communityName=?,communityProvince=? where communityId=?";
        Object obj[]={
                community.getCommunityAddress(),
                community.getCommunityAreaCode(),
                community.getCommunityCity(),
                community.getCommunityCoordinates(),
                community.getCommunityDistrict(),
                community.getCommunityId(),
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                community.getCommunityName(),
                community.getCommunityProvince(),
                community.getCommunityId()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public List<Community> selectAll(Map<String, String> map) {
        List<Community> list = new ArrayList<>();
        String sql = "select * from community where 1=1 ";
        List<Object> params = new ArrayList<>();
        String communityName = map.get("communityName");
        if(communityName!=null && !"".equals(communityName)){
            sql+=" and communityName like ?";
            params.add("%"+communityName+"%");
        }
        String startTime = map.get("startTime");
        if(startTime!=null && !"".equals(startTime)){
            sql+=" and createTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        String num = map.get("pageNum");
        if(num!=null && !"".equals(num)){
            sql+=" limit ?,?";
            int pageNum = Integer.parseInt(num);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                Community community = new Community(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getDate(9),
                        res.getInt(10),
                        res.getInt(11),
                        res.getString(12),
                        res.getInt(13),
                        res.getInt(14),
                        res.getString(15)
                );
                list.add(community);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int total = 0;
        String sql = "select count(1) from community where 1=1 ";
        List<Object> params = new ArrayList<>();
        String communityName = map.get("communityName");
        if(communityName!=null && !"".equals(communityName)){
            sql+=" and communityName like ?";
            params.add("%"+communityName+"%");
        }
        String startTime = map.get("startTime");
        if(startTime!=null && !"".equals(startTime)){
            sql+=" and createTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                total = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return total;
    }
}
